import { create } from 'zustand'
import { cartUsersService } from "@/services/cartUsersService";
import { message } from 'antd';
import type { List } from '@/types/common'
import type { SearchParams, CartUsersItem } from '../types/cartUsers'

export const useCartUsersStore = create((set) => ({
    usersData: {} as List<CartUsersItem>,
    /**  查询 */
    getCartUsers: async (params: SearchParams) => {

        set(() => ({ usersData: {} as List<CartUsersItem> }))

        const { code, data } = await cartUsersService.getCartUsersService(params)

        if (code) {
            message.error(`查询失败`)
        } else {
            // this.usersData = data
            set(() => ({ usersData: data as List<CartUsersItem> }))
        }
    },

    /**  新增 */
    createCartUsers: async (params: CartUsersItem) => {
        const { code } = await cartUsersService.createCartUsersService(params)

        if (code) {
            message.error(`添加失败`)
        } else {
            message.success(`添加成功`)
        }

        return { code }
    },

    /**  修改 */
    updateCartUsers: async (params: CartUsersItem) => {
        const { code } = await cartUsersService.updateCartUsersService(params)

        if (code) {
            message.error(`修改失败`)
        } else {
            message.success(`修改成功`)
        }

        return { code }
    },

    /**  删除 */
    deleteCartUsers: async (params: { userId: string | number }) => {
        const { code } = await cartUsersService.deleteCartUsersService(params)

        if (code) {
            message.error(`删除失败`)
        } else {
            message.success(`删除成功`)
        }

        return { code }

    },





}))